package com.hegongda.service.impl;

import com.hegongda.bean.Orders;
import com.hegongda.bean.PageBean;
import com.hegongda.bean.Product;
import com.hegongda.bean.User;
import com.hegongda.dao.AdminDao;
import com.hegongda.dao.ProductDao;
import com.hegongda.service.AdminService;
import com.hegongda.utils.BeanFactory;
import com.hegongda.utils.JdbcUtils;
import com.hegongda.utils.JedisUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import redis.clients.jedis.Jedis;

import java.io.File;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;
import java.util.UUID;

public class AdminServiceImpl implements AdminService {
    private AdminDao ad = BeanFactory.newInstance(AdminDao.class);
    @Override
    public int delNav(String cid) {
        int a = 0;
        try {
            a = ad.delNav(cid);
            Jedis jedis = JedisUtils.getJedis();
            jedis.del("category");
            jedis.close();
        } catch (SQLException e) {}
        return a;
    }

    @Override
    public int addNav(String cname) {
        int a = 0;
        try {
            String cid = UUID.randomUUID().toString().replace("-","");
            a = ad.addNav(cid,cname);
            Jedis jedis = JedisUtils.getJedis();
            jedis.del("category");
            jedis.close();
        } catch (SQLException e) {}
        return a;
    }

    @Override
    public int updNav(String cid,String cname) {
        int a = 0;
        try {
            a = ad.updNav(cid,cname);
            Jedis jedis = JedisUtils.getJedis();
            jedis.del("category");
            jedis.close();
        } catch (SQLException e) {}
        return a;
    }

    @Override
    public int addPriduct(Product product) {
        int a = 0;
        try {
            a = ad.addPriduct(product);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return a;
    }

    @Override
    public int delPriduct(String pid,String path) {
        int a = 0;
        try {
            Product oneProduct = ad.getOneProduct(pid);
            String paths =  path+ oneProduct.getPimage();
            File file = new File(paths);
            file.delete();
            a = ad.delPriduct(pid,oneProduct.getIid());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return a;
    }

    @Override
    public PageBean<Product> getPageProduct(int pageSize, int pagenum) {
        PageBean<Product> pb = new PageBean<Product>();
        try {
            //设置当前页数
            pb.setCurrentPage(pagenum);
            //设置每页显示条数
            pb.setPageSize(pageSize);
            //设置显示商品数据,从数据库查询
            //调用dao层,传递,当前页,每页显示条数,分类外键
            List<Product> pageProduct = ad.getPageProduct((pagenum - 1) * pageSize, pageSize);
            pb.setList(pageProduct);
            //设置商品总数量,数据库查询
            //调用dao层方法,传递分类外键
            long productCount = ad.getProductCount();
            pb.setTotalCount(productCount);
            //设置总页数
            int totalPage = (int) Math.ceil(productCount * 1.0 / pageSize);
            pb.setTotalPage(totalPage);
        }catch (SQLException e){e.printStackTrace();}
        return pb;
    }

    @Override
    public int updPFlag(int pflag,String pid) {
        int i = 0;
        try {
            i = ad.updPFlag(pflag, pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public PageBean<User> getPageUser(int pageSize, int pagenum) {
        PageBean<User> pb = new PageBean<User>();
        try {
            //设置当前页数
            pb.setCurrentPage(pagenum);
            //设置每页显示条数
            pb.setPageSize(pageSize);
            //设置显示商品数据,从数据库查询
            //调用dao层,传递,当前页,每页显示条数,分类外键
            List<User> pageProduct = ad.getPageUser((pagenum - 1) * pageSize, pageSize);
            pb.setList(pageProduct);
            //设置商品总数量,数据库查询
            //调用dao层方法,传递分类外键
            long productCount = ad.getUserCount();
            pb.setTotalCount(productCount);
            //设置总页数
            int totalPage = (int) Math.ceil(productCount * 1.0 / pageSize);
            pb.setTotalPage(totalPage);
        }catch (SQLException e){e.printStackTrace();}
        return pb;
    }

    @Override
    public int updUser(User user) {
        int a = 0;
        try {
            a = ad.updUser(user);
        } catch (SQLException e) {}
        return a;
    }

    @Override
    public int delUser(String uid) {
        int a = 0;
        try {
            a = ad.delNav(uid);
        } catch (SQLException e) {}
        return a;
    }

    @Override
    public User getOneUser(String uid) {
        User oneUser = null;
        try {
            oneUser = ad.getOneUser(uid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return oneUser;
    }

    @Override
    public PageBean<Orders> selAllOrder(int pageSize, int pagenum) {
        long ordersCount = 0;
        List<Orders> allOrder = null;
        try {
            ordersCount = ad.selOrdersCount();
            allOrder = ad.selAllOrder((pagenum - 1) * pageSize, pageSize);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        PageBean<Orders> pb = new PageBean<>();
        pb.setTotalCount(ordersCount);
        pb.setCurrentPage(pagenum);
        pb.setTotalPage((int) Math.ceil(ordersCount * 1.0 / pageSize));
        pb.setPageSize(pageSize);
        pb.setList(allOrder);

        return pb;
    }

    @Override
    public int editCourierNum(String oid, String couriernum) {
        int i = 0;
        try {
            i = ad.editCourierNum(oid, couriernum);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int editCourier(String oid, String couriernum) {
        int i = 0;
        try {
            i = ad.editCourier(oid, couriernum);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int addintroduce(String iid, String introduce) {
        int i = 0;
        try {
            i = ad.addintroduce(iid, introduce);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }
}
